package DBLayer;

/**
 * DBSupplier.java
 * @author Anders Mellon Skov Hermansen
 * @version 21. Marts 2012
 */
import ModelLayer.*;

import java.sql.*;
import java.util.ArrayList;

public class DBSupplier implements IFDBSupplier{

    private  Connection con;
    
    public DBSupplier() 
    {
      con = DBConnection.getInstance().getDBcon();
    }

    public ArrayList<Supplier> getAllSupplier(boolean retriveAssociation)
    {
        return miscWhere("", retriveAssociation);
    }

    public Supplier findSupplier(int suplid, boolean retriveAssociation)
    {   
    	String wClause = "  id = " + suplid + "";
        return singleWhere(wClause, retriveAssociation);
    }

    public Supplier searchSupplierId(int attValue, boolean retriveAssociation)
    {
        String wClause = "id = " + attValue + "";
        System.out.println("Search Supplier: " + wClause);
        return singleWhere(wClause, retriveAssociation);
    }
    
    public Supplier searchSupplierName(String attValue, boolean retriveAssociation)
    {
        String wClause = "name like '%" + attValue + "%'";
        System.out.println("Search Supplier " + wClause);
        return singleWhere(wClause, retriveAssociation);
    }

    public Supplier searchSupplierAddress(String attValue, boolean retriveAssociation)
    {
        String wClause = "address = '" + attValue + "'";
        System.out.println("Search Supplier " + wClause);
        return singleWhere(wClause, retriveAssociation);
    }

    public Supplier searchSupplierphone(int attValue, boolean retriveAssociation)
    {
        String wClause = "phone = " + attValue + "";
        System.out.println("Search Supplier " + wClause);
        return singleWhere(wClause, retriveAssociation);
    }
    
    public Supplier searchSupplieremail(int attValue, boolean retriveAssociation)
    {
        String wClause = "email = " + attValue + "";
        System.out.println("Search Supplier " + wClause);
        return singleWhere(wClause, retriveAssociation);
    }

    public int insertSupplier(Supplier supl)
    {
    	int nextId = GetMax.getMaxId("Select max(id) from Supplier");
        nextId = nextId + 1;
        System.out.println("next id = " +  nextId);
    	
       int rc = -1;
	   String query="INSERT INTO Supplier(id, name, address, country, phone, email)  VALUES(" +
	   		nextId  + ",'"  +
		    supl.getName()  + "','" +
            supl.getAddress() + "'," +
            supl.getCountry() + "," +
            supl.getPhone() + "," +
            supl.getEmail() + ")";
                     
      System.out.println("insert : " + query);		  					
      try{
            Statement stmt = con.createStatement();
	 	  	stmt.setQueryTimeout(5);
	 	  	rc = stmt.executeUpdate(query);
	 	  	stmt.close();
		}	
	 	catch(Exception ex){
	 	  	System.out.println("Insert exception in Supplier db: "+ex);
	   }
	   return(rc);
	}
	
	public int updateSupplier(Supplier supl)
	{
		Supplier suplObj  = supl;
		int rc=-1;
	  
		String query="UPDATE Supplier SET " +
		 	 "name ='" + suplObj.getName() + "', " +
		 	 "address ='" + suplObj.getAddress() + "', " +
		 	 "country =" + suplObj.getCountry() + ", " +
		 	 "phone =" + suplObj.getPhone() + "," +
		 	 "email =" + suplObj.getEmail() + " " +
		     " WHERE id = '" + suplObj.getId() + "'";
			
            System.out.println("Update query:" + query);
  		try
  		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 	 	rc = stmt.executeUpdate(query);
	 	 	stmt.close();
		}
	 	catch(Exception ex)
	 	{
	 	 	System.out.println("Update exception in Supplier db: "+ex);
	  	}
		return(rc);
	}
	
	public int delete(int id)
	{
               int rc=-1;
	  
	  	String query="DELETE FROM Supplier WHERE id = " +
				id + "";
                System.out.println(query);
	  	try
	  	{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 	  	rc = stmt.executeUpdate(query);
	 	  	stmt.close();
  		}
   	    catch(Exception ex)
   	    {
	 	  	System.out.println("Delete exception in Supplier db: "+ex);
   	    }
		return(rc);
	}
	
	private ArrayList<Supplier> miscWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		ArrayList<Supplier> suplomlist = new ArrayList<Supplier>();	
		
		String query =  buildQuery(wClause);
  
  		try
  		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
//			int snr=0;
			while( results.next() )
			{
				Supplier suplObj = new Supplier();
				suplObj = buildSupplier(results);	
				suplomlist.add(suplObj);	
			}
			stmt.close();
		}
	 	catch(Exception e)
	 	{
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
		return suplomlist;
	}
	
	private Supplier singleWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		Supplier suplObj = new Supplier();
	    String query =  buildQuery(wClause);
        System.out.println(query);
		try
		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() )
	 		{
	 			suplObj = buildSupplier(results);
	 			stmt.close();
			}
		
		}	
	 	catch(Exception e)
	 	{
	 		System.out.println("Query exception: "+e);
	 	}
		return suplObj;
	}

	private String buildQuery(String wClause)
	{
	    String query="SELECT * FROM Supplier";
		
		if (wClause.length()>0)
			query=query+" WHERE "+ wClause;
			
		return query;
	}

	private Supplier buildSupplier(ResultSet results)
      {   
		Supplier suplObj = new Supplier();
          try
          {
                suplObj.setId(results.getInt(1));
                suplObj.setName(results.getString(2));
                suplObj.setAddress(results.getString(3));
                suplObj.setCountry(results.getString(4));
                suplObj.setPhone(results.getString(5));
                suplObj.setEmail(results.getString(6));
          }
         catch(Exception e)
         {
             System.out.println("error in building the Supplier object");
         }
         return suplObj;
      }

	@Override
	public Supplier searchSupplierCountry(String country,
			boolean retriveAssociation) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Supplier searchSupplierEmail(String email,
			boolean retrieveAssociation) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Supplier searchSupplierPhone(int phone, boolean retriveAssociation) {
		// TODO Auto-generated method stub
		return null;
	}
}  
    

